/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.boot.web.reactive.config;

import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;

import com.iiifi.kite.boot.properties.KiteRequestLogProperties;
import com.iiifi.kite.boot.web.reactive.logger.ReactiveRequestLogFilter;
import com.iiifi.kite.boot.web.reactive.logger.RequestLogExclusiveRule;
import com.iiifi.kite.core.util.StringPool;
import com.iiifi.kite.core.util.StringUtils;

import lombok.RequiredArgsConstructor;

/**
 * webflux 日志拦截器
 *
 * @author kite@iiifi.com 花朝
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnProperty(value = "kite.log.request.enabled", havingValue = "true", matchIfMissing = true)
public class KiteRequestLogConfiguration {
    private final KiteRequestLogProperties kiteRequestLogProperties;

    @Bean
    @ConditionalOnMissingBean
    public ReactiveRequestLogFilter reactiveRequestLogFilter(RequestLogExclusiveRule exclusiveRule) {
        return new ReactiveRequestLogFilter(kiteRequestLogProperties, exclusiveRule);
    }

    /**
     * 默认的请求日志排除规则
     */
    @Configuration
    @ConditionalOnMissingBean(RequestLogExclusiveRule.class)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
    public static class DefaultRequestLogExclusiveRule implements RequestLogExclusiveRule {

        @Override
        public boolean excluded(String path) {
            return path.contains(StringPool.DOT) && !path.toLowerCase().endsWith(".jackson");
        }
    }

    /**
     * 排除 /actuator
     */
    @Configuration
    @RequiredArgsConstructor
    @ConditionalOnClass(WebEndpointProperties.class)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
    public static class WebEndpointRequestLogExclusiveRule extends DefaultRequestLogExclusiveRule {
        private static final String STATIC_PATH_PATTERN = "/**";
        private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
        private final WebEndpointProperties properties;
        private final WebFluxProperties webFluxProperties;

        @Override
        public boolean excluded(String path) {
            // 对 /actuator 排除
            String actuatorBasePath = properties.getBasePath();
            if (StringUtils.isNotBlank(actuatorBasePath) && path.startsWith(actuatorBasePath)) {
                return true;
            }
            // 如果设置了静态目录 例如：/static, 对静态文件排除
            String staticPathPattern = webFluxProperties.getStaticPathPattern();
            if (!STATIC_PATH_PATTERN.equals(staticPathPattern) &&
                    PATH_MATCHER.match(staticPathPattern, path)) {
                return true;
            }
            return super.excluded(path);
        }
    }
}
